package net.stream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.Security;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;

public class JCE
{
	public static void Zip() throws IOException
	{
		FileInputStream fin = new FileInputStream("Demo.zip");
		ZipInputStream gin = new ZipInputStream(fin);
		ZipEntry ze = null;
		int b = 0;
		while ((ze = gin.getNextEntry()) != null)
		{
			FileOutputStream fou = new FileOutputStream(ze.getName());
			while ((b = gin.read()) != -1)
			{
				fou.write(b);
			}
			fou.flush();
			fou.close();
		}
		gin.close();
	}
	public static void Digest(OutputStream out,InputStream in) throws NoSuchAlgorithmException, IOException
	{
		MessageDigest sha = MessageDigest.getInstance("SHA");
		DigestOutputStream dout = new DigestOutputStream(out, sha);
		byte[] buffer = new byte[128];
		while(true)
		{
			int bytesRead = in.read(buffer);
			if(bytesRead<0)
			{
				break;
			}
			dout.write(buffer, 0, bytesRead);
		}
		dout.flush();
		dout.close();
		dout.getMessageDigest().digest();
	}
	public static void Spider()
	{
		String infile = "secret.txt";
		String outfile = "secret.des";
		String password = "i have a spider";
		try
		{
			FileInputStream fin = new FileInputStream(infile);
			FileOutputStream fout = new FileOutputStream(outfile);
			
			Provider sunJce = new com.sun.crypto.provider.SunJCE();
			Security.addProvider(sunJce);
			
			char[] pbeKeyData = password.toCharArray();
			PBEKeySpec pbeKeySpec = new PBEKeySpec(pbeKeyData);
			SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
			SecretKey key = keyFactory.generateSecret(pbeKeySpec);
			
			Cipher phe = Cipher.getInstance("PBEWithMD5AndDES");
			phe.init(Cipher.ENCRYPT_MODE, key);
			CipherOutputStream cout = new CipherOutputStream(fout, phe);
			
			byte[] input = new byte[64];
			while(true)
			{
				int bytesRead = fin.read(input);
				if(bytesRead==-1)
				{
					break;
				}
				cout.write(input);
			}
			cout.flush();
			cout.close();
			fin.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException
	{
		Writer w = new OutputStreamWriter(System.out, "utf-8");
		char[] network = {'N','e','t','w','o','r','k'};
		try
		{
			w.write(network, 0, network.length);
			w.flush();
			w.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
